package com.yyfax.ph.personal.authority.gesture;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.yyfax.common.CLog;
import com.yyfax.common.form.LoginLayout;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.R;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.personal.authority.forgot.ForgotPwdActivity;
import com.yyfax.ph.personal.authority.verifypassword.VerifyPwdPresenter;

import butterknife.BindView;
import butterknife.OnClick;
import com.yyfax.ph.main.MainActivity;

/**
 * Created by laixj on 2017/3/6.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class ResetGesturesActivity extends ModifyGesturesActivity implements VerifyPwdPresenter.View {

	private final static String TAG = "ResetGesturesActivity";

	private static final int LOADER_ID_VERIFY_PWD = 901;
	private final static String EXTRA_BUSINESS = "extra_business";

	@BindView(R.id.password_layout)
	LoginLayout mPassword;
	@BindView(R.id.gestures_reset_container)
	LinearLayout mResetContainer;
	@BindView(R.id.gesture_modify_container)
	LinearLayout mGestureContainer;

	private VerifyPwdPresenter mPresenter;

	public static void start(Activity aty) {
		Intent i = new Intent(aty, ResetGesturesActivity.class);
		i.putExtra(EXTRA_BUSINESS, GESTURE_RESET);
		aty.startActivity(i);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(LOADER_ID_VERIFY_PWD, null, this);
	}

	@Override
	protected void onStop() {
		getSupportLoaderManager().destroyLoader(LOADER_ID_VERIFY_PWD);
		super.onStop();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void initContentView() {
		super.initContentView();
		mGestureContainer.setVisibility(View.GONE);
		mResetContainer.setVisibility(View.VISIBLE);
	}

	@OnClick({R.id.gestures_reset_forgot, R.id.gestures_reset_next})
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.gestures_reset_forgot:
				YJPH.hideInputWindow(this);
				ForgotPwdActivity.start(this);
				break;
			case R.id.gestures_reset_next:
				YJPH.getInstance().hideInputWindow(this);
				if (YJPH.getInstance().getPreferenceManager().isLoginSuccess()) {
					String password = mPassword.getText().toString().trim();
					if (TextUtils.isEmpty(password)) {
						showToast(R.string.input_password);
					} else {
						mPresenter.verifyPwd(password);
					}
				} else {
					MainActivity.start(this, true);
				}
				break;
		}
	}

	@Override
	public void onVerify() {
		GesturesPresenter.resetGestureCount();
		GesturesPresenter.writeStatus(false);
		YJPH.getInstance().removeChosenPattern();
		mResetContainer.setVisibility(View.GONE);
		mGestureContainer.setVisibility(View.VISIBLE);
	}

	@Override
	public void onVerifyFailure(String tips) {
		showToast(tips);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID_VERIFY_PWD:
				return new PresenterLoader<>(this, new VerifyPwdPresenter.Generator());
			default:
				break;
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		CLog.i(TAG, "LoginPresent onLoadFinished--");
		switch (loader.getId()) {
			case LOADER_ID_VERIFY_PWD:
				mPresenter = (VerifyPwdPresenter) data;
				mPresenter.attach(this);
				break;
			default:
				break;
		}
	}
}
